import { _decorator, Component, lerp, Node, Vec3 } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('CameraControl')
export class CameraControl extends Component {
    @property(Node) player: Node = null;
    private offset: Vec3 = new Vec3(0, 0,0);
    private pos: Vec3 = new Vec3(0, 0, 0);
    private cur: Vec3 = new Vec3(0, 0, 0);

    update(deltaTime: number) {
        //直接跟随
        // this.pos.x = this.player.position.x + this.offset.x;
        // this.pos.y = this.player.position.y + this.offset.y;
        // this.node.setPosition(this.pos);
        //逼近
        this.player.getPosition(this.cur);
        this.cur.add(this.offset);
        const x = this.node.position.x;
        const y = this.node.position.y;
        this.pos.x = lerp(x, this.cur.x, 2 * deltaTime);
        this.pos.y = lerp(y, this.cur.y, 2 * deltaTime);
        this.node.setPosition(this.pos);
    }
}


